var cheerio = require('cheerio');
var http = require('http');
var iconv = require('iconv-lite');

var url = 'http://www.ygdy8.net/html/gndy/dyzz/list_23_';
var index = 1;
var titles = [];

function getTitle(url, i){
    console.log("now catch No."+i+" page content");
    http.get(url+i+'.html', function (res) { 
        //获取字节数据，并将数据存入chunks数组
        var chunks = [];
        res.on('data', function(chunk){
            chunks.push(chunk);
        });

        res.on('end', function () {
            var html = iconv.decode(Buffer.concat(chunks), 'gb2312');
            var $ = cheerio.load(html, {decodeEntities : false});
            $('.co_content8 .ulink').each(function(idx, element){
                var $element = $(element);
                titles.push({
                    title:$element.text()
                });
            });
            if(i < 3){
                getTitle(url, ++index);
            }else{
                console.log(titles);
                console.log("ending...")
            }
            
          });
     });
     
}

function main(){
    console.log("starting...")
    getTitle(url, index);
}
main();